Imports System
Imports System.Net
Imports System.IO

Public Class WebRequestWrapper
    Inherits WebRequest

    Protected Const GZIP As String = "gzip"
    Private gzipRequest As Boolean
    Private wr As WebRequest


    Public Sub New(ByVal wrappedRequest As WebRequest, ByVal compressRequest As Boolean, ByVal acceptCompressedResponse As Boolean)
        wr = wrappedRequest
        gzipRequest = compressRequest
        If (gzipRequest) Then wr.Headers("Content-Encoding") = GZIP
        If (acceptCompressedResponse) Then wr.Headers("Accept-Encoding") = GZIP
    End Sub

    Public Overrides Property Method() As String
        Get
            Return wr.Method
        End Get
        Set(ByVal Value As String)
            wr.Method = Value
        End Set
    End Property

    Public Overrides ReadOnly Property RequestUri() As Uri
        Get
            Return wr.RequestUri
        End Get
    End Property

    Public Overrides Property Headers() As WebHeaderCollection
        Get
            Return wr.Headers
        End Get
        Set(ByVal Value As WebHeaderCollection)
            wr.Headers = Value
        End Set
    End Property

    Public Overrides Property ContentLength() As Long

        Get
            Return wr.ContentLength
        End Get
        Set(ByVal Value As Long)
            wr.ContentLength = Value
        End Set
    End Property

    Public Overrides Property ContentType() As String
        Get
            Return wr.ContentType
        End Get
        Set(ByVal Value As String)
            wr.ContentType = Value
        End Set
    End Property

    Public Overrides Property Credentials() As ICredentials
        Get
            Return wr.Credentials
        End Get
        Set(ByVal Value As ICredentials)
            wr.Credentials = Value
        End Set
    End Property

    Public Overrides Property PreAuthenticate() As Boolean
        Get
            Return wr.PreAuthenticate
        End Get
        Set(ByVal Value As Boolean)
            wr.PreAuthenticate = Value
        End Set
    End Property

    Private request_stream As Stream = Nothing

    Public Overrides Function GetRequestStream() As System.IO.Stream
        Return WrappedRequestStream(wr.GetRequestStream())
    End Function

    Public Overrides Function BeginGetRequestStream(ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        Return wr.BeginGetRequestStream(callback, state)
    End Function

    Public Overrides Function EndGetRequestStream(ByVal asyncResult As IAsyncResult) As System.IO.Stream
        Return WrappedRequestStream(wr.EndGetRequestStream(asyncResult))
    End Function

    Private Function WrappedRequestStream(ByVal requestStream As Stream) As Stream
        If (request_stream Is Nothing) Then
            request_stream = requestStream
            If (gzipRequest) Then request_stream = New ICSharpCode.SharpZipLib.GZip.GZipOutputStream(request_stream)
        End If
        Return request_stream
    End Function

    Public Overrides Function GetResponse() As WebResponse
        Return New WebResponseWrapper(wr.GetResponse())
    End Function

    Public Overrides Function BeginGetResponse(ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        Return wr.BeginGetResponse(callback, state)
    End Function

    Public Overrides Function EndGetResponse(ByVal asyncResult As IAsyncResult) As WebResponse
        Return New WebResponseWrapper(wr.EndGetResponse(asyncResult))
    End Function
End Class
